﻿using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using System.Collections.Generic;
using System;

namespace WentStateMachine
{
    [CustomEditor(typeof(StateMachineController))]
    public class StateMachineControllerGUI : Editor
    {
        StateMachineController controller;
        RuntimeAnimatorController animator;
        List<IStateMachineControllerGUIEx> exs = new List<IStateMachineControllerGUIEx>();

        private string namespaceForGenerateScript;
        private void OnEnable()
        {
            controller = (StateMachineController)target;
            if (!string.IsNullOrEmpty(controller.AnimatorControllerGUID))
            {
                var path = AssetDatabase.GUIDToAssetPath(controller.AnimatorControllerGUID);
                animator = AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(path);
            }

            findGuiExtension();
        }

        public override void OnInspectorGUI()
        {
            EditorGUILayout.LabelField(new GUIContent("Please selected a animator to generate StateMechineController"));
            var localanimator = (RuntimeAnimatorController)EditorGUILayout.ObjectField(animator, typeof(RuntimeAnimatorController), false);
            if (localanimator != animator)
            {
                var path = AssetDatabase.GetAssetPath(localanimator);
                var guid = AssetDatabase.AssetPathToGUID(path);
                controller.AnimatorControllerGUID = guid;
                animator = localanimator;
            }

            if (!string.IsNullOrEmpty(controller.AnimatorControllerGUID))
            {
                if (GUILayout.Button("ReGenerate"))
                {
                    generate();
                }
                GUILayout.Space(5);
                namespaceForGenerateScript = EditorGUILayout.TextField("NameSpace:", namespaceForGenerateScript);
                if (GUILayout.Button("Generate Missing StateBehaviour File"))
                {
                    generateScripts();
                }
            }

            foreach(var ex in exs)
            {
                ex.OnGUI(controller);
            }
        }

        private void findGuiExtension()
        {
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                var types = assembly.GetTypes();
                foreach (Type type in types)
                {
                    if (typeof(IStateMachineControllerGUIEx).IsAssignableFrom(type) && !type.IsInterface)
                    {
                        exs.Add((IStateMachineControllerGUIEx)Activator.CreateInstance(type));
                    }
                }
            }
        }

        private void generate()
        {
            new Generator(controller, (AnimatorController)animator).Generate();
        }

        private void generateScripts()
        {
            new StateScriptsGenerator(animator as AnimatorController, namespaceForGenerateScript).Generate();
        }
    }
}
